package cn.itenzo.controller;

import cn.itenzo.common.validate.LengthValidate;
import cn.itenzo.common.validate.NotBlankValidate;
import cn.itenzo.common.vo.RetMsg;
import cn.itenzo.good.model.GoodItem;
import cn.itenzo.good.service.ItemService;
import cn.itenzo.vo.ReqParam;
import cn.itenzo.vo.RetList;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.Result;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2017/5/21 0021.
 */
@Api(description = "分类")
@Controller
@RequestMapping(value = "item")
public class GoodItemController {


    private static final Logger logger = LoggerFactory.getLogger(GoodItemController.class);


    @Reference(version = "1.0.0")
    private ItemService itemService;

    @ApiOperation(value = "查询单个的分类")
    @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "Long")
    @ResponseBody
    @RequestMapping(value = "/q/{id}", method = RequestMethod.GET)
    public GoodItem queryItem(@PathVariable Long id) {
        logger.info("查询分类  {}", id);
        return itemService.queryItemById(id);
    }


    @ApiOperation(value = "删除单个的分类")
    @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "Long")
    @ResponseBody
    @RequestMapping(value = "/d/{id}", method = RequestMethod.GET)
    public Integer deteleItem(@PathVariable Long id) {
        logger.info("删除分类 {}", id);
        Integer integer = itemService.deleteItemById(id);
        logger.info("已经删除分类 {}", id);
        return integer;
    }


    @ApiOperation(value = "添加分类")
    @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "Long")
    @ResponseBody
    @RequestMapping(value = "/a", method = RequestMethod.GET)
    public RetMsg SaveItem(GoodItem goodItem) {
        logger.info("保存分类分类 {}", goodItem.toString());

        Result result = FluentValidator.checkAll()
                .on(goodItem.getmStoreid() + "", new NotBlankValidate("门店ID"))

                .on(goodItem.getItemName(), new LengthValidate(2, 10, "分类名称"))
                .doValidate()
                .result(ResultCollectors.toSimple());
        if (!result.isSuccess()) {
            logger.warn(result.getErrors().get(0));
            return RetMsg.error(result.getErrors().get(0));
        }

        Integer integer = itemService.save(goodItem);
        logger.info("保存分类 {}", goodItem.toString());
        return RetMsg.success("保存成功", goodItem);
    }



    @ApiOperation(value = "列出分类")
    @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "Long")
    @ResponseBody
    @RequestMapping(value = "/l", method = RequestMethod.POST)
    public RetList ItemList(Long id,@RequestBody ReqParam param) {
        logger.info("列出分类 stroid{}", id);

        Result result = FluentValidator.checkAll()
                .on(id + "", new NotBlankValidate("门店ID"))


                .doValidate()
                .result(ResultCollectors.toSimple());
        if (!result.isSuccess()) {
            logger.warn(result.getErrors().get(0));
            return null;
        }
        GoodItem goodItem = new GoodItem();
        goodItem.setmStoreid(id);
        int page = param.getPage();
        PageInfo<GoodItem> goodItemPageInfo = itemService.queryItemByPage(goodItem, page, param.getLimit(), param.getOrderCluse());
        RetList retList = new RetList(goodItemPageInfo.getTotal(),goodItemPageInfo.getList());


        return retList;
    }
}
